#include "dbconnection.h"
#include <QSqlQuery>

QSharedPointer<DbConnection> DbConnection::_instance;

DbConnection::DbConnection()
{
}

DbConnection::~DbConnection()
{
}

QSharedPointer<DbConnection> DbConnection::instance()
{
    if(_instance.isNull())
    {
        _instance = QSharedPointer<DbConnection>(new DbConnection);
    }
    return _instance;
}

bool DbConnection::setupConnection(QSharedPointer<Settings> settings)
{
    QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
    database.setHostName(settings->hostname);
    database.setDatabaseName(settings->dbname);
    database.setUserName(settings->login);
    database.setPassword(settings->password);
    return database.open();
}

bool DbConnection::isAlive()
{
    QSqlQuery query;
    return query.exec("SELECT 1;");
}


QSqlDatabase DbConnection::getDb()
{
    return QSqlDatabase::database();
}
